package com.hyb.hybmodulessearch.controller;

import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.biz.SAPageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.hyb.hybmodulessearch.domain.ActivityResponseVo;
import com.hyb.hybmodulessearch.domain.ActivitySearchVo;
import com.hyb.hybmodulessearch.domain.Document;
import com.hyb.hybmodulessearch.mapper.es.SearchVoMapper;
import com.ruoyi.common.core.constant.ActivityConstant;
import com.ruoyi.common.core.constant.SortConstants;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.Base64Utils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.sign.Base64;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.domain.Activity;
import io.micrometer.core.instrument.binder.BaseUnits;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.UndeclaredThrowableException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/activity")
public class ActivitySearchController extends BaseController {

    @Autowired
    SearchVoMapper searchVoMapper;

    @Autowired
    RedisTemplate<String,String> redisTemplate;



    @InnerAuth
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Activity activity){
        ActivityResponseVo activityResponseVo = new ActivityResponseVo();
        BeanUtils.copyProperties(activity,activityResponseVo);
        activityResponseVo.setThrumb(activity.getThumbCount());
        activityResponseVo.setHot(activity.getHotCount());
        activityResponseVo.setIntergal( activity.getPreIntegral());
        activityResponseVo.setStartTime(activity.getStartTime());
        activityResponseVo.setLastTime(activity.getLastTime());

        String introduce=redisTemplate.opsForValue().get(ActivityConstant.ACTIVITY_INTRODUCE_KEY+activity.getId());

        byte[] decode = Base64.decode(introduce);

        if (decode!=null){
            activityResponseVo.setIntroduce(new String(decode));
        }else{
            activityResponseVo.setIntroduce("");
        }
        if(activity.getStatus()!=null) {
            //活动状态,变成文字
            if (ActivityConstant.CREATE_STATUS.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.COLLAGE_STATUS_NAME);
            } else if (ActivityConstant.TO_APPROVE_STATUS.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.TO_APPROVE_STATUS_NAME);
            } else if (ActivityConstant.SCHOOL_APPROVE.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.SCHOOL_APPROVE_NAME);
            } else if (ActivityConstant.APPROVE_FINAL.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.APPROVE_FINAL_NAME);
            } else if (ActivityConstant.COLLAGE_STATUS.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.COLLAGE_STATUS_NAME);
            } else if (ActivityConstant.PUBLISH_STATUS.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.PUBLISH_STATUS_NAME);
            } else if (ActivityConstant.SINGUP_STATUS.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.SINGUP_STATUS_NAME);
            } else if (ActivityConstant.SINGUP_FINAL.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.SINGUP_FINAL_NAME);
            } else if (ActivityConstant.LASTED_STATUS.equals(activity.getStatus())) {
                activityResponseVo.setStatus(ActivityConstant.LASTED_STATUS_NAME);
            }

        }


        //处理是否支持请假
        if (activity.getPrepareField1().equals(String.valueOf(ActivityConstant.SUPPORT_LEAVE))) {
            activityResponseVo.setPrepareField1(ActivityConstant.SUPPORT_LEAVE_NAME);
        } else {
            activityResponseVo.setPrepareField1(ActivityConstant.NO_SUPPORT_LEAVE_NAME);
        }
        try{
            Integer integer = searchVoMapper.updateById(activityResponseVo);
            return integer==1? AjaxResult.success():AjaxResult.error("更新失败");
        }catch (UndeclaredThrowableException e){
            searchVoMapper.insert(activityResponseVo);
        }catch (Exception e){
            throw new GlobalException("elasticSearch更新失败,请联系管理员处理");
        }
        return AjaxResult.success();
    }

//    上传查询数据接口
    @InnerAuth
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody Activity activity){
        ActivityResponseVo activityResponseVo = new ActivityResponseVo();
        BeanUtils.copyProperties(activity,activityResponseVo);
        activityResponseVo.setThrumb(activity.getThumbCount()==null?0:activity.getThumbCount());
        activityResponseVo.setHot(activity.getHotCount()==null?0:activity.getHotCount());
        activityResponseVo.setIntergal(activity.getPreIntegral()==null? new BigDecimal(0) :activity.getPreIntegral());
        activityResponseVo.setStartTime(activity.getStartTime());
        activityResponseVo.setLastTime(activity.getLastTime());
        String introduce=redisTemplate.opsForValue().get(ActivityConstant.ACTIVITY_INTRODUCE_KEY+activity.getId());

        byte[] decode = Base64.decode(introduce);

        if (decode!=null){
            activityResponseVo.setIntroduce(new String(decode));
        }else{
            activityResponseVo.setIntroduce("");
        }


        activityResponseVo.setStatus("刚创建,新建的活动");
        //处理是否支持请假
        if (activity.getPrepareField1().equals(String.valueOf(ActivityConstant.SUPPORT_LEAVE))){
            activityResponseVo.setPrepareField1(ActivityConstant.SUPPORT_LEAVE_NAME);
        }else{
            activityResponseVo.setPrepareField1(ActivityConstant.NO_SUPPORT_LEAVE_NAME);
        }
        Integer insert = searchVoMapper.insert(activityResponseVo);

        return insert==1?AjaxResult.success():AjaxResult.error("插入失败");

    }


    @PostMapping("/search")
    public AjaxResult search(@RequestBody ActivitySearchVo searchVo){



        LambdaEsQueryWrapper<ActivityResponseVo> wrapper = new LambdaEsQueryWrapper<>();

        if(StringUtils.isEmpty(searchVo.getHot())
                && StringUtils.isEmpty(searchVo.getIntergal())
                && StringUtils.isEmpty(searchVo.getThrumb())
                && StringUtils.isEmpty(searchVo.getLastTime())
                && StringUtils.isEmpty(searchVo.getStartTime())
        ){
            //如果排序规则都为空,默认已活动id排序
            wrapper.orderByAsc(ActivityResponseVo::getId);
        }

        if (!StringUtils.isEmpty(searchVo.getHot())){
            if(SortConstants.ASEC_SORT.equals(searchVo.getHot())){
                wrapper.orderByDesc(ActivityResponseVo::getHot);
            }else{
                wrapper.orderByAsc(ActivityResponseVo::getHot);
            }
        }

        if(!StringUtils.isEmpty(searchVo.getIntergal())){
            if(SortConstants.ASEC_SORT.equals(searchVo.getIntergal())){
                wrapper.orderByDesc(ActivityResponseVo::getIntergal);
            }else{
                wrapper.orderByAsc(ActivityResponseVo::getIntergal);
            }
        }

        if(!StringUtils.isEmpty(searchVo.getThrumb())){
            if(SortConstants.ASEC_SORT.equals(searchVo.getThrumb())){
                wrapper.orderByDesc(ActivityResponseVo::getThrumb);
            }else {
                wrapper.orderByAsc(ActivityResponseVo::getThrumb);
            }
        }

        //包装时间
        if(!StringUtils.isEmpty(searchVo.getStartTime())){
            if(SortConstants.ASEC_SORT.equals(searchVo.getStartTime())){
                wrapper.orderByDesc(ActivityResponseVo::getStartTime);
            }else{
                wrapper.orderByAsc(ActivityResponseVo::getStartTime);
            }
        }

        if(!StringUtils.isEmpty(searchVo.getLastTime())){
            if(SortConstants.ASEC_SORT.equals(searchVo.getLastTime())){
                wrapper.orderByDesc(ActivityResponseVo::getLastTime);
            }else{
                wrapper.orderByAsc(ActivityResponseVo::getLastTime);
            }
        }


        //包装分类
        if(!StringUtils.isEmpty(searchVo.getClassification())){
            wrapper.eq("classification.keyword",searchVo.getClassification());
        }

        //包装名称
        if (!StringUtils.isEmpty(searchVo.getName())){
            wrapper.like("name.keyword",searchVo.getName());
        }
        if (!StringUtils.isEmpty(searchVo.getTitle())){
            wrapper.like("title.keyword",searchVo.getTitle());
        }
        if (!StringUtils.isEmpty(searchVo.getIntroduce())){
            wrapper.like("introduce.keyword",searchVo.getIntroduce());
        }
        if (!StringUtils.isEmpty(searchVo.getNumber())){
            wrapper.like("number.keyword",searchVo.getNumber());
        }
        if (!StringUtils.isEmpty(searchVo.getPosition())){
            wrapper.like("position.keyword",searchVo.getPosition());
        }
        if(!StringUtils.isEmpty(searchVo.getVisitType())){
            wrapper.like("visitType.keyword",searchVo.getVisitType());
        }
        if (!StringUtils.isEmpty(searchVo.getStatus())){
            wrapper.like("status.keyword",searchVo.getStatus());
        }
        if (!StringUtils.isEmpty(searchVo.getVisitRange())){
            wrapper.like("visitRange.keyword",searchVo.getVisitRange());
        }
        if (!StringUtils.isEmpty(searchVo.getLabel())){
            wrapper.like("label.keyword",searchVo.getLabel());
        }
        if (!StringUtils.isEmpty(searchVo.getPrepareField1())) {
            wrapper.like("prepareField1.keyword",searchVo.getPrepareField1());
        }
        if(!StringUtils.isEmpty(searchVo.getPrepareField2())){
            wrapper.like("prepareField2.keyword",searchVo.getPrepareField2());
        }
        if (!StringUtils.isEmpty(searchVo.getLevel())){
            wrapper.like("level.keyword",searchVo.getLevel());
        }
        //包装搜索框内容
//        if(!StringUtils.isEmpty(searchVo.getContent())){
//            wrapper.match(ActivityResponseVo::getContent,searchVo.getContent());
//        }

        EsPageInfo<ActivityResponseVo> documentPageInfo = searchVoMapper.pageQuery(wrapper,searchVo.getPageNum(),searchVo.getPageSize());

        Map<String,Object> map=new HashMap<>();
        map.put("list",documentPageInfo.getList());
        map.put("total",documentPageInfo.getTotal());
        map.put("pages",documentPageInfo.getPages());
        return AjaxResult.success().put("map",map);
    }
}
